﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PortalCurriculo.Domain;
using PortalCurriculo.Util.CustomControls;
using PortalCurriculo.Util.Translation;
using PortalCurriculo.WebUI.Helpers;

namespace PortalCurriculo.WebUI
{
    public partial class SiteMaster : MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CultureHelper.Cultura = new CultureInfo("pt-BR");

            Translator.FolderResource = Server.MapPath("~/") + ConfigurationManager.AppSettings["PastaIdiomas"];
            Translator.Language = CultureHelper.Cultura;
            //Translator.TranslatePage(Page);

            var usuario = (Usuario)SessionHelper.Session(SessionHelper.SessionName.UsuarioLogado);

            PreencherLinks(usuario);

            if ((Request.IsAuthenticated && usuario != null) ||
                !VerifyAuthenticatedPages()) return;

            Response.Redirect("~/Account/Login.aspx");
        }

        private void PreencherLinks(Usuario usuario)
        {
            if (usuario == null)
            {
                mnuLeft.Items.Clear();
            }
            else
            {
                var links = (List<Link>)SessionHelper.Session(SessionHelper.SessionName.LinksUsuario);

                if (links == null || links.Count == 0)
                {
                    if (PreencherLinksProfissional() == null)
                        if (PreencherLinksSelecionador() == null)
                            PreencherLinksEmpresa();

                    links = (List<Link>)SessionHelper.Session(SessionHelper.SessionName.LinksUsuario);
                }

                mnuLeft.Items.Clear();

                if (links == null) return;
                
                foreach (var link in links)
                {
                    var menuItem = new MenuItem(link.Descricao, link.Descricao, null, link.URL);
                    mnuLeft.Items.Add(menuItem);
                }

                mnuLeft.DataBind();
            }
        }

        private List<Link> PreencherLinksProfissional()
        {
            var profissional = (Profissional)SessionHelper.Session(SessionHelper.SessionName.ProfissionalLogado);

            if (profissional != null)
            {
                List<Link> links = new Link().SelecionarPorTipoUsuario(TipoUsuario.Tipo.Profissional, Link.DivLocation.Left);
                SessionHelper.Session(SessionHelper.SessionName.LinksUsuario, links);
                return links;
            }

            return null;
        }

        private List<Link> PreencherLinksEmpresa()
        {
            var empresa = (Empresa)SessionHelper.Session(SessionHelper.SessionName.EmpresaLogada);
            var usuario = (Usuario)SessionHelper.Session(SessionHelper.SessionName.UsuarioLogado);

            if (empresa != null || usuario.Pessoa.PessoaJuridica != null)
            {
                List<Link> links = new Link().SelecionarPorTipoUsuario(TipoUsuario.Tipo.Empresa, Link.DivLocation.Left);
                SessionHelper.Session(SessionHelper.SessionName.LinksUsuario, links);
                return links;
            }

            return null;
        }

        private List<Link> PreencherLinksSelecionador()
        {
            var selecionador = (Selecionador)SessionHelper.Session(SessionHelper.SessionName.SelecionadorLogado);
            if (selecionador != null)
            {
                List<Link> links = new Link().SelecionarPorTipoUsuario(TipoUsuario.Tipo.Selecionador, Link.DivLocation.Left);
                SessionHelper.Session(SessionHelper.SessionName.LinksUsuario, links);
                return links;
            }

            return null;
        }

        private bool VerifyAuthenticatedPages()
        {
            var xmlReader = new DataSet();
            xmlReader.ReadXml(MapPath("~/XML/AuthenticatedPages.xml"));

            DataTable pages = xmlReader.Tables[0];

            return (from DataRow page in pages.Rows
                    where page[0].ToString().ToUpper().Equals(Request.FilePath.ToUpper())
                    select true).FirstOrDefault();
        }

        protected void HeadLoginStatus_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            SessionHelper.ClearAllSessions();
        }

        protected void uppCarregando_Load(object sender, EventArgs e)
        {
            //var popup = (Popup)uppCarregando.FindControl("popup");
            //popup.Show();   
        }
    }
}
